/*
 * TypeFrame.java
 *
 * Created on __DATE__, __TIME__
 */

package com.insigma.yanwenlong.bookkeeping.gui;

import java.sql.SQLException;
import java.util.List;

import javax.swing.DefaultComboBoxModel;

import com.insigma.yanwenlong.bookkeeping.pojo.Type;
import com.insigma.yanwenlong.bookkeeping.service.TypeService;
import com.insigma.yanwenlong.bookkeeping.service.TypeServiceImpl;

/**
 *
 * @author  __USER__
 */
public class TypeFrame extends javax.swing.JFrame {
	private TypeService typeService = new TypeServiceImpl();
	private int directionId = 0;

	//private DefaultComboBoxModel dcbmBig;
	//private DefaultComboBoxModel dcbmSmall;
	/** Creates new form TypeFrame */
	public TypeFrame() {

		this.setTitle("类别维护");
		initComponents();
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setLocationRelativeTo(null);
		//初始化类别
		initType();
	}

	private void initType() {
		//directionId为1时表示支出，其他表示收入
		if (rabtnOutcome.isSelected()) {
			directionId = 1;
		}
		try {
			List<Type> types = typeService.findAllBigTypes(directionId);
			DefaultComboBoxModel dcbmBig = new DefaultComboBoxModel();
			for (Type type : types) {
				dcbmBig.addElement(type);
			}
			cobBig.setModel(dcbmBig);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		initSmallType();
	}

	private void initSmallType() {
		Type type2 = (Type) cobBig.getSelectedItem();
		int id = type2.getId();
		try {

			List<Type> types = typeService.findAllSmallTypes(id, directionId);
			DefaultComboBoxModel dcbmSmall = new DefaultComboBoxModel();
			for (Type type : types) {
				dcbmSmall.addElement(type);
			}
			cobSmall.setModel(dcbmSmall);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		buttonGroup1 = new javax.swing.ButtonGroup();
		jLabel1 = new javax.swing.JLabel();
		rabtnOutcome = new javax.swing.JRadioButton();
		rabtnIncome = new javax.swing.JRadioButton();
		jLabel2 = new javax.swing.JLabel();
		cobBig = new javax.swing.JComboBox();
		cobSmall = new javax.swing.JComboBox();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		jLabel1.setText("\u6536\u652f\u65b9\u5411\uff1a");

		buttonGroup1.add(rabtnOutcome);
		rabtnOutcome.setSelected(true);
		rabtnOutcome.setText("\u652f\u51fa");
		rabtnOutcome.addItemListener(new java.awt.event.ItemListener() {
			public void itemStateChanged(java.awt.event.ItemEvent evt) {
				rabtnOutcomeItemStateChanged(evt);
			}
		});

		buttonGroup1.add(rabtnIncome);
		rabtnIncome.setText("\u6536\u5165");
		rabtnIncome.addItemListener(new java.awt.event.ItemListener() {
			public void itemStateChanged(java.awt.event.ItemEvent evt) {
				rabtnIncomeItemStateChanged(evt);
			}
		});

		jLabel2.setText("\u7c7b\u522b\u540d\u79f0\uff1a");

		cobBig.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
				"Item 1", "Item 2", "Item 3", "Item 4" }));
		cobBig.addItemListener(new java.awt.event.ItemListener() {
			public void itemStateChanged(java.awt.event.ItemEvent evt) {
				cobBigItemStateChanged(evt);
			}
		});

		cobSmall.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
				"Item 1", "Item 2", "Item 3", "Item 4" }));

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addGap(20, 20, 20)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addComponent(
																				jLabel2)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				cobBig,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				javax.swing.GroupLayout.PREFERRED_SIZE))
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addComponent(
																				jLabel1)
																		.addGap(
																				18,
																				18,
																				18)
																		.addComponent(
																				rabtnOutcome)))
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addGap(
																				30,
																				30,
																				30)
																		.addComponent(
																				rabtnIncome))
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addGap(
																				18,
																				18,
																				18)
																		.addComponent(
																				cobSmall,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				javax.swing.GroupLayout.PREFERRED_SIZE)))
										.addContainerGap(40, Short.MAX_VALUE)));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addGap(23, 23, 23)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel1)
														.addComponent(
																rabtnOutcome)
														.addComponent(
																rabtnIncome))
										.addGap(41, 41, 41)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel2)
														.addComponent(
																cobBig,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																cobSmall,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addContainerGap(156, Short.MAX_VALUE)));

		pack();
	}// </editor-fold>
	//GEN-END:initComponents
	
	//选中大类后选中其对应的小类
	private void cobBigItemStateChanged(java.awt.event.ItemEvent evt) {
		initSmallType();
	}

	//选中收入
	private void rabtnIncomeItemStateChanged(java.awt.event.ItemEvent evt) {
		if (rabtnIncome.isSelected()) {
			directionId = 0;
		}
		initOutcomeOrIncome(directionId);
		initSmallType();
	}

	//选中支出
	private void rabtnOutcomeItemStateChanged(java.awt.event.ItemEvent evt) {
		if (rabtnOutcome.isSelected()) {
			directionId = 1;
		}
		initOutcomeOrIncome(directionId);
		initSmallType();
	}

	private void initOutcomeOrIncome(int directionId2) {
		try {
			List<Type> types = typeService.findAllBigTypes(directionId);
			DefaultComboBoxModel dcbmBig = new DefaultComboBoxModel();
			for (Type type : types) {
				dcbmBig.addElement(type);
			}
			cobBig.setModel(dcbmBig);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new TypeFrame().setVisible(true);
			}
		});
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.ButtonGroup buttonGroup1;
	private javax.swing.JComboBox cobBig;
	private javax.swing.JComboBox cobSmall;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JRadioButton rabtnIncome;
	private javax.swing.JRadioButton rabtnOutcome;
	// End of variables declaration//GEN-END:variables

}